/*
 * Copyright 2016-2017 lei.xu<xulei2008xulei@163.com>.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.san21go.jfinfunc.time;

import org.joda.time.DateTime;
import org.san21go.jfinfunc.AbstractFunction;
import org.san21go.jfinfunc.FinDict;
import org.san21go.jfinfunc.FinFuncRuntimeException;
import org.san21go.jfinfunc.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description: YEARFRAC 函数 返回 start_date 和 end_date 之间的天数占全年天数的百分比。 使用
 *               YEARFRAC 工作表函数可判别某一特定条件下全年效益或债务的比例。
 * @version V1.0
 */
public class YEARFRAC extends AbstractFunction<Double> {
	public static final String HUNDSUN_VERSION = "@system 理财资产管理系统V2.2 @version v2.2.0.1 @lastModiDate 2016年6月7日 @describe  Excel函数支持";
	public static final Logger logger = LoggerFactory.getLogger(YEARFRAC.class);
	/**
	 * 必需。 一个代表开始日期的日期。
	 */
	private String start_date;
	/**
	 * 必需。 一个代表终止日期的日期。
	 */
	private String end_date;
	/**
	 * 可选。 要使用的日计数基准类型。
	 */
	private String basis;

	public YEARFRAC() {
		super();
	}

	public YEARFRAC(String start_date, String end_date, String basis) {
		super();
		this.start_date = start_date;
		this.end_date = end_date;
		this.basis = basis;
	}

	/**
	 * @see com.san21go.jexcelfunc.IExprFunction#evaluate()
	 */
	public Double evaluate() throws FinFuncRuntimeException {
		if (basis == null || basis.length() == 0) {
			basis = FinDict.BASIS_0;
		}
		double ret = 0D;
		if (FinDict.BASIS_0.equals(basis)) {
			int months = DateUtil.diffMonth(end_date, start_date);
			start_date = DateUtil.formatDate(start_date).plusMonths(months).toString(DateUtil.DEFAULT_FORMAT_DATE);

			int diffDays = DateUtil.diffDate(end_date, start_date) + months * 30;

			ret = Double.parseDouble(diffDays + "") / 360;

		} else if (FinDict.BASIS_1.equals(basis)) {
			int diffDays = DateUtil.diffDate(end_date, start_date);
			int years = DateUtil.diffYear(end_date, start_date);
			String tmpEndDate = DateUtil.formatDate(start_date).plusYears(years + 1)
					.toString(DateUtil.DEFAULT_FORMAT_DATE);
			int diff2 = DateUtil.diffDate(tmpEndDate, start_date);

			// 判断是否包含了闰年的闰日
			if (DateUtil.formatDate(tmpEndDate).year().isLeap()) {
				DateTime tmpLeap = new DateTime(DateUtil.formatDate(tmpEndDate).getYear(), 2, 29, 0, 0, 0);
				if (DateUtil.formatDate(end_date).getMillis() < tmpLeap.getMillis()) {
					diff2 = diff2 - 1;
				}
			}

			ret = Double.parseDouble(diffDays + "") / diff2 * (years + 1);
		} else if (FinDict.BASIS_2.equals(basis)) {
			int diffDays = DateUtil.diffDate(end_date, start_date);
			ret = Double.parseDouble(diffDays + "") / 360;
		} else if (FinDict.BASIS_3.equals(basis)) {
			int diffDays = DateUtil.diffDate(end_date, start_date);
			ret = Double.parseDouble(diffDays + "") / 365;
		} else if (FinDict.BASIS_4.equals(basis)) {
			int months = DateUtil.diffMonth(end_date, start_date);
			start_date = DateUtil.formatDate(start_date).plusMonths(months).toString(DateUtil.DEFAULT_FORMAT_DATE);
			int diffDays = DateUtil.diffDate(end_date, start_date) + months * 30;

			ret = Double.parseDouble(diffDays + "") / 360;
		} else {
			throw new UnsupportedOperationException("日计数基准不受支持！");
		}

		return ret;
	}

}
